const express = require("express");
const router = express.Router();
const pool = require("../pool");
require("../public/js/util");
//接口类型：订单查询接口
//接口地址：http://127.0.0.1:3000/v1/orders/detail
//请求方式：GET
router.get("/detail", (req, res, next) => {
  let obj = req.query;
  pool.query(
    "select * from hsb_order_detail where user_id=? order by oid desc",
    [obj.uid],
    (err, result) => {
      if (err) {
        next(err);
        return;
      }
      if (result.length > 0) {
        res.send({ code: 200, msg: "suc", result });
      } else {
        res.send({ code: 400, msg: "fail" });
      }
    }
  );
});

//接口类型：订单生成接口
//接口地址：http://127.0.0.1:3000/v1/orders/production
//请求方式：get
//传参说明：uid:用户ID，addressId:用户地址编号 price:产品价格 fid:产品家族编号 img：图片路径 pid:产品编号
router.get("/production", (req, res, next) => {
  let obj = req.query;
  let r1 = Math.floor(Math.random() * 10);
  let r2 = Math.floor(Math.random() * 10);
  let sysDate = new Date().Format("yyyyMMddhhmmss");
  let createDate = new Date().Format("yyyy-MM-dd hh:mm:ss");
  let orderIdVal = r1 + sysDate + r2;
  let status = '0';
  let sql = "insert into hsb_order_detail values(null,?,?,?,?,?,?,?,?)";
  pool.query(sql,[obj.uid,orderIdVal,obj.price, createDate,status,obj.img,obj.pid,obj.Rmethod], (err, result) => { 
    if (err) { 
      next(err)
      return
    }
    if (result.affectedRows > 0) {
      res.send({ code: 200, msg: 'suc' })
    } else { 
      res.send({code:400,msg:'fail'})
    }
  })
});
module.exports = router;
